package marmalius.player;

import marmalius.client;
import marmalius.item.ItemService;
import marmalius.item.ItemType;
import marmalius.tools.Config;

public class Inventory extends Storage {
	public Inventory(client client) {
		this.client = client;
		maxItem = 28;
		items = new Item[maxItem];
		for (int i = 0; i < maxItem; i++)
			items[i] = null;
		this.client = client;
	}
	
	public boolean hasItemIdAtIndex(int itemId, int index) {
		if (index < 0 || index >= maxItem)
			return false;
		if (items[index] != null)
			return items[index].getItemTemplate().getId() == itemId;
		return false;
	}
	
	public boolean hasItem(int itemID) {
		for (int i = 0; i < maxItem; i++) {
			if (items[i] != null && items[i].getItemTemplate().getId() == itemID)
				return true;
		}
		return false;
	}
	
	public boolean hasItemCount(int itemID, int count) {
		return getItemCount(itemID) >= count;
	}
	
	public Item getPickAxe() { //pioche
		for (int i = 0; i < maxItem; i++) {
			if (items[i] != null && items[i].getItemTemplate().isType(ItemType.PICKAXE))
				return items[i];
		}
		return null;
	}
	
	public Item getAxe() { //hache
		for (int i = 0; i < maxItem; i++) {
			if (items[i] != null && items[i].getItemTemplate().isType(ItemType.AXE))
				return items[i];
		}
		return null;
	}
	
	public int getTotalSlot(int itemId) {
		int result = 0;
		for (int i = 0; i < maxItem; i++) {
			if (items[i] != null && items[i].getItemTemplate().getId() == itemId)
				result++;
		}
		return result;
	}
	
	
	
	public void moveItem(client client, int from, int to) { //window 3724
		if (from < 0 || from >= maxItem || to < 0 || to >= maxItem || from == to) {
			updateClientInventorySlot(from);
			updateClientInventorySlot(to);
			return ;
		}
		
		Item tmp = items[to];
		items[to] = items[from];
		items[from] = tmp;
		
		updateClientInventorySlot(from);
		updateClientInventorySlot(to);
	}
	
	public void addItemFromBank(int itemID, int count) {
		addItem(itemID, count);
		client.getInventory().updateClientInventory(5064);
	}
	
	public int removeItemToBank(int itemID, int slot, int count) {
		int result = 0;
		if (items[slot] != null && items[slot].getItemTemplate().getId() == itemID) {
			result = count - removeItem(itemID, count);
			
			client.getInventory().updateClientInventory(5064);
		}
		return result;
	}
	
	public int addItem(int itemID, int count) {
		if (count == 0)
			return count;
		if (ItemService.getInstance().getItemTemplate(itemID).isStackable()) {
			for (int i = 0; i < maxItem; i++) {
				if (items[i] != null && items[i].getItemTemplate().getId() == itemID) {
					if (items[i].getCount() + count > Config.MAXSTACK) {
						count -= Config.MAXSTACK - items[i].getCount();
						items[i].setCount(Config.MAXSTACK);
					}
					else {
						items[i].setCount(items[i].getCount() + count);
						count = 0;
					}
					updateClientInventorySlot(i);
					return count;
				}
			}
			for (int i = 0; i < maxItem; i++) {
				if (items[i] == null && count > 0) {
					if (count > Config.MAXSTACK) {
						count -= Config.MAXSTACK;
						items[i] = new Item(itemID, Config.MAXSTACK);
					}
					else {
						items[i] = new Item(itemID, count);
						count = 0;
					}
					updateClientInventorySlot(i);
					return count;
				}
			}
		}
		for (int i = 0; i < maxItem; i++) {
			if (items[i] == null && count > 0) {
				items[i] = new Item(itemID, 1);
				updateClientInventorySlot(i);
				count--;
			}
		}
		
		return count;
	}
	
	public boolean haveSpaceForItem(int itemID, int count) {
		if (count == 0)
			return true;
		int index = -1;
		
		if (ItemService.getInstance().getItemTemplate(itemID).isStackable()) {
			for (int i = 0; i < maxItem; i++) {
				if (items[i] != null && items[i].getItemTemplate().getId() == itemID) {
					index = i;
					break;
				}
			}
		}
		if (index != -1)
			return items[index].getCount() + count <= Config.MAXSTACK;
		else {
			for (int i = 0; i < maxItem; i++) {
				if (items[i] == null && count > 0)
					count--;
			}
			return count == 0;
		}
	}
	
	public int removeItem(int itemID, int count) {
		if (count <= 0)
			return 0;
		
		if (ItemService.getInstance().getItemTemplate(itemID).isStackable()) {
			for (int i = 0; i < maxItem; i++) {
				if (items[i] != null && items[i].getItemTemplate().getId() == itemID) {
					if (items[i].getCount() - count < 0) {
						count -= items[i].getCount();
						items[i].setCount(0);
						items[i] = null;
					}
					else {
						items[i].setCount(items[i].getCount() - count);
						count = 0;
					}
					updateClientInventorySlot(i);
					return count;
				}
			}
		}
		for (int i = 0; i < maxItem; i++) {
			if (items[i] != null && items[i].getItemTemplate().getId() == itemID && count > 0) {
				items[i].setCount(0);
				items[i] = null;
				updateClientInventorySlot(i);
				count--;
			}
		}
		return count;
	}
	
	public Item removeSlotItem(int itemID) {
		for (int i = 0; i < maxItem; i++) {
			if (items[i] != null && items[i].getItemTemplate().getId() == itemID) {
				Item removeItem = items[i];
				items[i] = null;
				updateClientInventorySlot(i);
				return removeItem;
			}
		}
		return null;
	}
	
	public int getItemCount(int itemID) {
		int total = 0;
		for (int i = 0; i < maxItem; i++) {
			if (items[i] != null && items[i].getItemTemplate().getId() == itemID)
				total += items[i].getCount();
		}
		return total;
	}
	
	public int getCoinCount() {
		for (int i = 0; i < maxItem; i++) {
			if (items[i] != null && items[i].getItemTemplate().getId() == Config.COINID)
				return items[i].getCount();
		}
		return 0;
	}
	
	private void updateClientInventorySlot(int index) {
		if (items[index] != null) {
			client.outStream.createFrameVarSizeWord(34);
			client.outStream.writeWord(3214);
			client.outStream.writeByte(index);
			
			client.outStream.writeWord(items[index].getItemTemplate().getClientId());
			if (items[index].getCount() > 254) {
				client.outStream.writeByte(255);
				client.outStream.writeDWord(items[index].getCount());
			}
			else
				client.outStream.writeByte(items[index].getCount());
			client.outStream.endFrameVarSizeWord();
		}
		else {
			client.outStream.createFrameVarSizeWord(34);
			client.outStream.writeWord(3214);
			client.outStream.writeByte(index);
			
			client.outStream.writeWord(0);
			client.outStream.writeByte(0);
			client.outStream.endFrameVarSizeWord();
		}
	}
	
	public void updateClientInventory(int window) { //window 3214
		client.outStream.createFrameVarSizeWord(53);
		client.outStream.writeWord(window);
		client.outStream.writeWord(28);

		for (int i = 0; i < maxItem; i++) {
			if (items[i] != null) {
				if (items[i].getCount() > 254) {
					client.outStream.writeByte(255);
					client.outStream.writeDWord_v2(items[i].getCount());
				}
				else
					client.outStream.writeByte(items[i].getCount());
				client.outStream.writeWordBigEndianA(items[i].getItemTemplate().getClientId()); // item id
			}
			else {
				client.outStream.writeByte(0);
				client.outStream.writeWordBigEndianA(0);
			}
		}
		client.outStream.endFrameVarSizeWord();
	}
}
